package service;

import com.alibaba.fastjson.JSONObject;
import entity.GameRoom;
import entity.Player;
import entity.User;

public class Message {

    public static final String LOGINFAIL;
    public static final String COOKIELOGINFAIL;
    public static final String REGISTFAIL;
    public static final String USERLOST;
    public static final String PASSWORDCHANGED;
    public static final String PASSWORDNOTCHANGED;
    public static final String NICKNAMECHANGED;
    public static final String NICKNAMENOTCHANGED;
    public static final String HEADPICCHANGED;
    public static final String HEADPICNOTCHANGED;

    public static final String SWAP_HOST;         //交换房主
    public static final String CHALLENGER_LEAVE; //挑战者退出
    public static final String HOST_LEAVE;      //房主退出
    public static final String CLEANJSONMESSAGE;
    public static final String EXITJSONMESSAGE;
    public static final String DRAWMESSAGE;
    public static final String PLAYERCONFIRM;//玩家确认战果

    public static final String CONFIGROOMSUCCEED;

    public static final String ENTER_ROOM;
    public static final String CREATE_ROOM;
    public static final String JOINED_ROOM;
    public static final String ENTER_ROOM_FAIL;

    static {
        JSONObject json = new JSONObject();
        json.put("type", "playerConfirm");
        PLAYERCONFIRM = json.toJSONString();
        json.put("type", "loginFail");
        LOGINFAIL = json.toJSONString();
        json.put("type", "cookieLoginFail");
        COOKIELOGINFAIL = json.toJSONString();
        json.put("type", "registFail");
        REGISTFAIL = json.toJSONString();
        json.put("type", "userLost");
        USERLOST = json.toJSONString();
        json.put("type", "passchanged");
        PASSWORDCHANGED = json.toJSONString();
        json.put("type", "passNotchanged");
        PASSWORDNOTCHANGED = json.toJSONString();
        json.put("type", "nickchanged");
        NICKNAMECHANGED = json.toJSONString();
        json.put("type", "nickNotchanged");
        NICKNAMENOTCHANGED = json.toJSONString();
        json.put("type", "headPicChanged");
        HEADPICCHANGED = json.toJSONString();
        json.put("type", "headPicNotChanged");
        HEADPICNOTCHANGED = json.toJSONString();
        json.put("type", "swap_host");
        SWAP_HOST = json.toJSONString();
        json.put("type", "challenger_leave");
        CHALLENGER_LEAVE = json.toJSONString();
        json.put("type", "host_leave");
        HOST_LEAVE = json.toJSONString();
        json.put("type", "clean");
        CLEANJSONMESSAGE = json.toJSONString();
        json.put("type", "exit");
        EXITJSONMESSAGE = json.toJSONString();
        json.put("type", "draw");
        DRAWMESSAGE = json.toJSONString();
        json.put("type", "configRoomSucceed");
        CONFIGROOMSUCCEED = json.toJSONString();
        json.put("type", "enterRoom");
        ENTER_ROOM = json.toJSONString();
        json.put("type", "creatRoom");
        CREATE_ROOM = json.toJSONString();
        json.put("type", "joinedRoom");
        JOINED_ROOM = json.toJSONString();
        json.put("type", "enterRoomFail");
        ENTER_ROOM_FAIL = json.toJSONString();
    }

    public static String loginSuccess(String redirectTo) {
        JSONObject json = new JSONObject();
        json.put("type", "loginSuccess");
        json.put("redirect", redirectTo);
        return json.toJSONString();
    }

    public static String registSuccess(String redirectTo) {
        JSONObject json = new JSONObject();
        json.put("type", "registSuccess");
        json.put("redirect", redirectTo);
        return json.toJSONString();
    }

    public static String loginFail() {
        return LOGINFAIL;
    }
    public static String cookieLoginFail() {
        return COOKIELOGINFAIL;
    }

    public static String registFail() {
        return REGISTFAIL;
    }

    public static String logOut(String redirectTo) {
        JSONObject json = new JSONObject();
        json.put("type", "logOut");
        json.put("redirect", redirectTo);
        return json.toJSONString();
    }

    public static String passwordChanged() {return PASSWORDCHANGED;}
    public static String passwordNotChanged() {return PASSWORDNOTCHANGED;}

    public static String nicknameChanged() {return NICKNAMECHANGED;}
    public static String nicknameNotChanged() {return NICKNAMENOTCHANGED;}

    public static String headPicChanged() {return HEADPICCHANGED;}
    public static String headPicNotChanged() {return HEADPICNOTCHANGED;}
    /*
    private Long    id;
    private String  name;
    private String  nick;
    private String  passwordHash;
    private String  headPic;

    private Integer win;
    private Integer lost;
    private Integer run;
    private Long    score;
     */
    public static String userInfo(User u, Long rank, Long rankSize) {
        if(u == null) return USERLOST;

        JSONObject json = new JSONObject();

        json.put("type", "userGet");
        json.put("userid", u.getId());
        json.put("username", u.getName());
        json.put("usernick", u.getNick());
        json.put("userheadPic", u.getHeadPic());
        json.put("win", u.getWin());
        json.put("lost", u.getLost());
        json.put("run", u.getRun());
        json.put("score", u.getScore());

        json.put("rank", rank);
        json.put("rankSize", rankSize);

        return json.toJSONString();
    }

    public static String userRank(Long rank, Long rankSize) {
        JSONObject json = new JSONObject();

        json.put("type", "userGetRank");

        json.put("rank", rank);
        json.put("rankSize", rankSize);

        return json.toJSONString();
    }

    private static void addPlayerInfo(JSONObject json, Player player, String userPrefix) {
        if(player == null) return;
        User uinfo = player.getUserInfo();
        if(uinfo != null) {
            json.put(userPrefix + "UID", uinfo.getId());
            json.put(userPrefix + "UName", uinfo.getName());
            if(uinfo.getNick() != null) json.put(userPrefix + "UNick", uinfo.getNick());
            else json.put(userPrefix + "UNick", " ");
            if(uinfo.getHeadPic() != null) json.put(userPrefix + "HeadPic", uinfo.getHeadPic());
            else json.put(userPrefix + "HeadPic", null);
        } else json.put(userPrefix + "UID", null);
    }

    public static String challengerJoin(Player p) {
        JSONObject json = new JSONObject();
        json.put("type", "challenger_join");
        addPlayerInfo(json, p, "challenger");
        return json.toJSONString();
    }

    private static void addRoomAbstract(JSONObject json, GameRoom room) {
        json.put("roomId", room.getId());
        json.put("roomTitle", room.getTitle());
        json.put("roomLocked", (room.getPasswordHash() == null? Boolean.valueOf(false): Boolean.valueOf(true)));
        json.put("roomReady", room.getReady());
        json.put("roomPlayerCount", room.getPlayerCount());
        addPlayerInfo(json, room.getHost(), "host");
        addPlayerInfo(json, room.getChallenger(), "challenger");
    }

    public static JSONObject getRoomAbstractJson(GameRoom room) {
        JSONObject json = new JSONObject();
        json.put("type", "room_abstract");
        addRoomAbstract(json, room);
        return json;
    }

    public static String getRoomAbstract(GameRoom room) {
        return getRoomAbstractJson(room).toJSONString();
    }

    public static String getRoomDetail(GameRoom room) {
        JSONObject json = new JSONObject();
        json.put("type", "room_detail");
        addRoomAbstract(json, room);
        int i = 0;
        for(Player visitor : room.getVisitors()) {
            addPlayerInfo(json, visitor, "visitor" + i);
        }
        return json.toJSONString();
    }

    public static String redirect(String addr) {
        JSONObject json = new JSONObject();
        json.put("type", "redirect");
        json.put("redirect", addr);
        return json.toJSONString();
    }
}
